home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
vol_100
/
151_01
/
ed3.c
< prev
next >
Wrap
Text File
|
1985-03-09
|
18KB
|
949 lines
/* ED3.C */
#include "ed0.c"
#include "ed1.ccc"
int readfile = -1; /* separate read and write files */
int writefile = -1; /* -1 for inactive, file channel no. for active */
char rfilename[SYSFNMAX]; /* file names */
char wfilename[SYSFNMAX];
char databuf[MAXLEN1]; /* single buffer replaces redundant buffers */
fileclear() /* initializes filenames to zero length */
{
rfilename[0] = EOS;
wfilename[0] = EOS;
}
append(args) char *args;
{
int file;
int n;
int topline;
char locfn[SYSFNMAX];
if (name1(args,locfn)==ERR) {
return;
}
if (locfn[0]==EOS) {
message("no file argument");
return;
}
if ((file=sysopen(locfn,"r"))==ERR) {
message("file not found");
return;
}
while ((n=readline(file,databuf,MAXLEN))>=0) {
if (n>MAXLEN) {
message("line truncated");
n=MAXLEN;
}
if (bufins(databuf,n)==ERR) {
break;
}
if (bufdn()==ERR) {
break;
}
}
sysclose(file);
topline=max(1,bufln()-SCRNL2);
bufout(topline,2,SCRNL2);
bufgo(topline);
}
change(args,begin,end) char *args; int begin,end;
{
char oldline[MAXLEN1];
char newline[MAXLEN1];
char oldpat[MAXLEN1];
char newpat[MAXLEN1];
int from, to, col, n, k;
if (get2args(args,&from,&to)==ERR) {
if (check2mark(begin,end,&from,&to)==ERR){
sysabort();
return;
}
}
scr_chr_attr(SETINTENSE);
fmtsout("search mask ? ",0);
getcmnd(oldpat,15);
fmtcrlf();
if(oldpat[0]==EOS) {
return;
}
pmtline();
scr_chr_attr(SETINTENSE);
fmtsout("change mask ? ",0);
getcmnd(newpat,15);
fmtcrlf();
while (from<=to) {
if (chkkey()==YES) {
break;
}
if (bufgo(from++)==ERR) {
sysabort();
break;
}
if (bufatbot()==YES) {
sysabort();
break;
}
n=bufgetln(oldline,MAXLEN);
n=min(n,MAXLEN);
oldline[n]=EOS;
if (oldpat[0]=='^') {
if (amatch(oldline,oldpat+1,NO)==YES) {
k=replace(oldline,newline,
oldpat+1,newpat,0);
if (k==ERR) {
sysabort();
return;
}
fmtcrlf();
putdec(bufln(),5);
fmtsout(newline,5);
outdeol();
bufrepl(newline,k);
}
continue;
}
if ((col=amatch(oldline,oldpat,YES))>=0) {
k=replace(oldline,newline,
oldpat,newpat,col);
if (k==ERR) {
sysabort();
return;
}
fmtcrlf();
putdec(bufln(),5);
fmtsout(newline,5);
outdeol();
bufrepl(newline,k);
}
}
fmtcrlf();
}
clear()
{
if (chkbuf()==YES) {
outclr();
outxy(0,SCRNL1);
bufnew();
message("buffer cleared");
return(YES);
}
else{
return(NO);
}
}
delete(args,begin,end) char *args; int begin,end;
{
int from, to;
if(get2args(args,&from,&to)==ERR) {
if (check2mark(begin,end,&from,&to)==ERR){
sysabort();
return;
}
}
if (from>to) {
return;
}
if (edpick(from,to)==OK){
message ("deleted lines saved in pick buffer");
}
if (bufgo(from)==ERR) {
return;
}
if (bufndel(to-from+1)==ERR) {
sysabort();
return;
}
bufout(bufln(),1,SCRNL1);
}
find()
{
return(suurch(bufln()+1,HUGE,YES));
}
list(args,begin,end) char *args; int begin,end;
{
int n;
int from, to, line, oldline;
oldline=bufln();
if (get2args(args,&from,&to)==ERR) {
if (check2mark(begin,end,&from,&to)==ERR) {
sysabort();
return;
}
}
line=from;
while (line<=to) {
fmtassn(NO);
if (chkkey()==YES) {
break;
}
fmtassn(YES);
if (bufgo(line++)!=OK) {
break;
}
if (bufatbot()) {
sysabort();
break;
}
n=bufgetln(databuf,MAXLEN1);
n=min(n,MAXLEN);
databuf[n]=CR;
fmtsout(databuf,0);
fmtcrlf();
}
fmtassn(NO);
bufgo(oldline);
}
extract(args,begin,end) char *args; int begin,end;
/* write indicated line range to writefile */
{
/*char args1[SCRNW1];
char *argp;
int file;
char locfn[SYSFNMAX]; */
int oldline, from, to, n;
if (writefile == -1) {
message("file not opened");
return;
}
oldline = bufln();
if (get2args(args,&from,&to)==ERR) {/* from and to are line range */
if (check2mark(begin,end,&from,&to)==ERR){
return;
}
}
/* message("enter file name"); */ /* request the file name */
/* getcmnd(args1,0);
argp=skipbl(args1);
if (syschkfn(argp)==ERR) {
return;
}
syscopfn(argp,locfn);
if (locfn[0]==EOS) {
message("no file argument");
return;
}
if ((file=sysopen(locfn,"w"))==ERR){
return;
} */
if (bufgo(from)==ERR){
/* sysclose(file); */
return;
}
while ((bufatbot()==NO)&(bufln()<=to)) { /* do the writing */
n=bufgetln(databuf,MAXLEN);
n=min(n,MAXLEN);
if (pushline(writefile,databuf,n)==ERR){
break;
}
if (bufdn()==ERR) {
break;
}
}
bufgo(oldline);
/* sysclose(file); */ /* close the file */
}
openf (args,flag) char *args; int flag;
{
/* open a file for reading */
char locfn [SYSFNMAX];
int n;
int file;
int topline;
if (readfile > 0) { /* check for open readfile */
message("read file still open");
return;
}
if (flag==YES) {
if (name1(args,locfn)==ERR) {
return;
}
}
else {
if (name3(args,locfn)==ERR) {
return;
}
}
if (locfn[0]==EOS) {
message("no file argument");
return;
}
if (chkbuf()==NO) {
return;
}
if ((file=sysopen(locfn,"r"))==ERR) {
message("file not found");
return;
}
syscopfn(locfn, rfilename); /* make the file name the readfile */
readfile = file;
pmtrfile(rfilename);
bufnew();
}
getit (args) char *args; /* new: add n lines to buffer if room */
{
int n;
int topline;
int nlines,npoint;
if (readfile == -1) {
message ("no read file");
return;
}
pmtrfile(rfilename);
if (getarg(args,&nlines)==ERR) {
return;
}
if (nlines<1) {
return;
}
if (bufgo(HUGE)==ERR) {
return;
}
npoint=1;
while (npoint<=nlines) { /* add the lines */
npoint++;
if ((n=readline(readfile,databuf,MAXLEN))>=0) {
if (n>MAXLEN) {
message("line truncated");
n=MAXLEN;
}
if (buf1ins(databuf,n)==ERR) {
bufgo(1);
topline=max(1,bufln()-SCRNL2);
bufout(topline,2,SCRNL2);
bufgo(topline);
return;
}
if (bufdn()==ERR) {
break;
}
}
else {
npoint = HUGE; /* reached end of file - close it */
sysclose(readfile);
readfile = -1;
rfilename[0] = EOS;
pmtrfile(rfilename);
}
}
bufgo(1);
topline=max(1,bufln()-SCRNL2);
bufout(topline,2,SCRNL2);
bufgo(topline);
}
rest (args) char *args; /* new: try to read rest of readfile into buffer */
{
int n;
int topline;
if (readfile == -1) {
message ("no read file");
return;
}
pmtrfile(rfilename);
/* if buffer has been changed and not saved, give option of clearing, */
/* otherwise add to end of buffer */
if(bufchng()==YES) {
scr_chr_attr(SETINTENSE);
fmtsout("buffer not saved. clear? ",0);
scr_chr_attr(SETFAINT);
pmtline();
if (tolower(syscout(syscin()))=='y'){
outclr();
outxy(0,SCRNL1);
bufnew();
message("buffer cleared");
}
else {
bufgo(HUGE);
}
}
else{
outclr();
outxy(0,SCRNL1);
bufnew();
message("buffer cleared");
}
while ((n=readline(readfile,databuf,MAXLEN))>=0) {
if (n>MAXLEN) {
message("line truncated");
n=MAXLEN;
}
if (buf1ins(databuf,n)==ERR) {
bufgo(1);
topline=max(1,bufln()-SCRNL2);
bufout(topline,2,SCRNL2);
bufgo(topline);
return;
}
if (bufdn()==ERR) {
break;
}
}
sysclose(readfile); /* close file if all read */
readfile = -1;
rfilename[0] = EOS;
pmtrfile(rfilename);
bufgo(1);
topline=max(1,bufln()-SCRNL2);
bufout(topline,2,SCRNL2);
bufgo(topline);
}
rename(args) char *args; /* new: change name of writefile */
{
if (writefile == -1) {
message("no write file - call name");
return;
}
sysclose(writefile);
writefile = -1;
wfilename[0]=EOS;
pmtwfile(wfilename);
if (name0(args,wfilename)==ERR) {
return;
}
pmtwfile(wfilename);
}
name(args) char *args; /* altered definition: name the writefile */
{
if (writefile > 0) {
message("write file open");
return;
}
if (name0(args,wfilename)==ERR){
return;
}
pmtwfile(wfilename);
}
name0(args,wfilename) char *ar